<template>
  <view class="container">
    <view class="alert">
      <text>设备计划</text>
    </view>
    <view class="myForm">
      <uni-forms
        ref="valiForm"
        :rules="rules"
        v-model="formData"
        label-position="top"
        label-width="150"
      >
        <uni-forms-item label="巡检项目" required name="patrolItemIds">
          <SelectCheckbox
            multiple
            :localdata="cData1"
            v-model="formData.patrolItemIds"
            @change="change"
          />
        </uni-forms-item>
        <uni-forms-item label="巡检周期" required name="patrolCycle">
          <uni-data-select
            placeholder="请选择"
            :localdata="cData2"
            v-model="formData.patrolCycle"
          />
        </uni-forms-item>
        <uni-forms-item label="巡检日期" required name="date">
          <uni-datetime-picker
            type="daterange"
            :start="new Date().getTime()"
            v-model="formData.date"
          />
        </uni-forms-item>
        <uni-forms-item label="巡检人" required name="patrolUserIds">
          <template v-slot:label>
            <view class="myLabel">
              <view><text class="star">*</text></view>
              <uni-icons
                type="plus-filled"
                size="30"
                color="#409EFF"
                @click="onLocation('/pages/user/select?multiple=1')"
              />
            </view>
          </template>
          <view
            :style="{
              color: formData.userName ? '#555' : '#888',
              marginTop: '10rpx',
            }"
            >{{ formData.userName || '点击右侧图标选择' }}</view
          >
        </uni-forms-item>
      </uni-forms>
    </view>
    <view class="btnBox">
      <button class="btn" type="primary" @click="onSubmit('valiForm')">
        提交
      </button>
    </view>
  </view>
</template>

<script>
import { netPatrolPlan, getPatrolItem, getPatrolPlanCycle } from '@/api/patrol';
import SelectCheckbox from '@/components/SelectCheckbox/index';
export default {
  components: {
    SelectCheckbox,
  },
  data() {
    return {
      // 字典数据
      cData1: [],
      cData2: [],
      // 表单数据
      formData: {
        patrolCycle: '',
        patrolItemIds: [],
        patrolUserIds: [],
      },
      // 规则
      rules: {
        applicationType: {
          rules: [
            {
              required: true,
              errorMessage: '请选择内容',
            },
          ],
        },
        spaceId: {
          rules: [
            {
              required: true,
              errorMessage: '请选择内容',
            },
          ],
        },
        location: {
          rules: [
            {
              required: true,
              errorMessage: '请输入内容',
            },
          ],
        },
        specificSituation: {
          rules: [
            {
              required: true,
              errorMessage: '请输入内容',
            },
          ],
        },
      },
    };
  },
  watch: {
    'formData.date': {
      handler(newVal) {},
      immediate: false,
      deep: true,
    },
  },
  onLoad(query) {
    getPatrolItem().then((res) => {
      this.cData1 = res.data.map((item) => {
        return {
          label: item.itemName,
          text: item.itemName,
          value: item.patrolItemId,
        };
      });
    });
    getPatrolPlanCycle().then((res) => {
      this.cData2 = res.data.map((item) => {
        return {
          label: item.dictLabel,
          text: item.dictLabel,
          value: item.dictValue,
        };
      });
    });
  },
  onShow() {
    this.$bus.on('addUserData', (res) => {
      if (res && res.length !== 0) {
        this.formData.userName = res.map((item) => item.nickName).join();
        this.formData.patrolUserIds = res.map((item) => item.userId);
      }
    });
  },
  onUnload() {
    this.$bus.off('addUserData');
  },
  methods: {
    change(e) {
      console.log('e:', e);
    },
    // 提交
    onSubmit(ref) {
      this.$refs[ref]
        .validate()
        .then((res) => {
          console.log('success', res);
          // loading提示
          uni.showLoading({ title: '操作中' });
          // 提交接口
          let params = {
            ...res,
            startDate: res.date[0],
            endDate: res.date[1],
          };
          // "isExpired": ""
          netPatrolPlan(params).then((res) => {
            uni.hideLoading();
            uni.showToast({
              title: `提交成功`,
            });
            // 重置
            this.onReset();
          });
        })
        .catch((err) => {
          console.log('err', err);
        });
    },
    // 重置表单
    onReset() {
      this.formData = this.$options.data().formData;
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - var(--window-bottom) - 200rpx);
  padding: 0rpx 36rpx;
  padding-bottom: calc(var(--window-bottom) + 200rpx);
  display: flex;
  flex-direction: column;

  .alert {
    padding: 16rpx;
    margin: 24rpx 0;
    text-align: center;
    background: #ddeafa;
    border-radius: 12rpx;
    text {
      font-size: 32rpx;
      font-weight: 600;
      color: #3a84ff;
    }
  }

  .myForm {
    padding: 24rpx;
    background: #fff;
    border-radius: 12rpx;

    ::v-deep .uni-forms-item__label {
      width: 35%;
      color: #333333;
      font-size: 28rpx;
      font-weight: bold;
      position: relative;

      &::after {
        // content: "";
        width: 100%;
        height: 1rpx;
        background: #000;
        position: absolute;
        left: 14rpx;
        bottom: 20rpx;
      }
    }

    .myLabel {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .text {
        color: #333333;
        font-size: 28rpx;
        font-weight: bold;
      }
      .star {
        color: #dd524d;
      }
    }
  }

  .btnBox {
    position: fixed;
    left: 36rpx;
    right: 36rpx;
    bottom: calc(var(--window-bottom) + 60rpx);
    .btn {
      width: 100%;
      height: 92rpx;
      font-size: 40rpx;
      color: #ffffff;
      line-height: 92rpx;
      background: #3681ff;
    }
  }
}
</style>
